{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测值: [ 66  82  79  98 112  96  86  89 104  84]\n",
      "真实值: [ 92 102  86 110 130  99  96 102 105  92]\n",
      "平方损失值: 447.69153479025357\n"
     ]
    }
   ],
   "source": [
    "import numpy as np # type: ignore\n",
    "\n",
    "x = np.array([56, 72, 69, 88, 102, 86, 76, 79, 94, 74])\n",
    "y = np.array([92, 102, 86, 110, 130, 99, 96, 102, 105, 92])\n",
    "\n",
    "\n",
    "\n",
    "# 使用 w0=10, w1=1 的预测值\n",
    "y_pred = 10 + 1 * x\n",
    "print(\"预测值:\", y_pred)\n",
    "print(\"真实值:\", y)\n",
    "\n",
    "\n",
    "def square_loss(x: np.ndarray, y: np.ndarray, w0: float, w1: float):\n",
    "    \"\"\"平方损失函数\"\"\"\n",
    "    loss = sum(np.square(y - (w0 + w1 * x)))\n",
    "    return loss\n",
    "\n",
    "# 假设模型参数为 w0=10, w1=1\n",
    "loss = square_loss(x, y, w0=41.33509168550616, w1=0.7545842753077117)\n",
    "print(f\"平方损失值: {loss}\")  # 输出损失值，用于后续参数优化"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
